package com.xm.controller.interceptor;

import com.xm.common.utils.JSONResult;
import com.xm.common.utils.JsonUtils;
import com.xm.common.utils.RedisOperator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;

public class MyInterceptor implements HandlerInterceptor {

    @Autowired
    public RedisOperator redis;

    public static final String USER_REDIS_SESSION = "user_redis_session";

    /**
     * 拦截请求
     * @param request
     * @param response
     * @param o
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        String userId = request.getHeader("userId");
        String userToken = request.getHeader("userToken");
        if(!StringUtils.isEmpty(userId) && !StringUtils.isEmpty(userToken)){
            String uniqueToken = redis.get(USER_REDIS_SESSION + ":" +userId);
            if(StringUtils.isEmpty(uniqueToken)){
                System.out.println("请登录...");
                returnErrorResponse(response,new JSONResult().errorTokenMsg("请重新登录..."));
                return false;
            }

            if(!uniqueToken.equals(userToken)){
                System.out.println("不能在两个地方同时登录哦...");
                returnErrorResponse(response,new JSONResult().errorTokenMsg("勿多地登录..."));
                return false;
            }
        }else{
            System.out.println("请登录...");
            returnErrorResponse(response,new JSONResult().errorTokenMsg("请登录..."));
            return false;
        }
        return true;
    }

    /**
     * 渲染视图之前
     * @param httpServletRequest
     * @param httpServletResponse
     * @param o
     * @param modelAndView
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    /**
     * 渲染视图之后
     * @param httpServletRequest
     * @param httpServletResponse
     * @param o
     * @param e
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }

    public void returnErrorResponse(HttpServletResponse response, JSONResult result) throws IOException {
        OutputStream out = null;
        try {
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/json");
            out = response.getOutputStream();
            out.write(JsonUtils.objectToJson(result).getBytes("utf-8"));
            out.flush();
        }finally {
            if(out != null){
                out.close();
            }
        }
    }
}
